package com.lu.pojo.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author luzhenfang
 * @date 2020 05 24
 * @description lock锁
 */
public class TestLock {
    public static void main(String[] args) {
        Lock lock = new Lock();
        new Thread(lock).start();
        new Thread(lock).start();
        new Thread(lock).start();
    }

}


class Lock implements Runnable{
     int tickNums = 10;
     // lock 锁
    private final ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {

        while (true){
            try {
                // 加锁
                lock.lock();
                if(tickNums>0){
                    Thread.sleep(100);
                    System.out.println(tickNums--);
                }else{
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                // 解锁
                lock.unlock();
            }
        }
    }
}